<template>
  <div class="my">
    <!-- 顶部蓝色背景 -->
    <van-cell class="my-userinfo">
      <!-- 上传图片 -->
      <van-uploader slot="icon" :after-read="onAfterRead">
        <!-- 头像icon -->
        <van-image round width="76px" height="76px" fit="cover" :src="userInfo.photo" />
      </van-uploader>

      <!-- 用户名 -->
      <template slot="title">{{userInfo.name}}</template>

      <!-- 申请认证 -->
      <van-button v-if="!userInfo.is_media" slot="label" size="mini" round type="info">申请认证</van-button>
      <van-tag v-else slot="label" type="warning">已认证</van-tag>

      <!-- 今天阅读 -->
      <div class="my-read">
        <van-icon size="25" name="description" />
        <div>
          今日阅读
          <br />5分钟
        </div>
      </div>
    </van-cell>

    <!-- 动态、关注、粉丝 -->
    <div class="my-numinfo">
      <div>
        {{userInfo.art_count}}
        <br />动态
      </div>
      <div>
        {{userInfo.follow_count}}
        <br />关注
      </div>
      <div>
        {{userInfo.fans_count}}
        <br />粉丝
      </div>
    </div>

    <!-- 收藏、历史、wtkk -->
    <van-grid :column-num="3" :border="false">
      <van-grid-item text="收藏">
        <van-icon color="#EB5859" slot="icon" name="location-o" />
      </van-grid-item>
      <van-grid-item text="历史">
        <van-icon color="#FFA127" slot="icon" name="like-o" />
      </van-grid-item>
      <van-grid-item text="wtkk">
        <van-icon color="#6C92FF" slot="icon" name="fire-o" />
      </van-grid-item>
    </van-grid>

    <!-- 消息通知 -->
    <van-cell-group>
      <van-cell to="/profile" title="个人资料" is-link />
      <van-cell title="消息通知" is-link />
      <van-cell title="实名认证" is-link />
    </van-cell-group>

    <van-cell-group>
      <van-cell to="/fankui" title="用户反馈" is-link />
      <van-cell to="/xiaozhi" title="小智同学" is-link />
      <van-cell title="系统设置" is-link />
    </van-cell-group>

    <!-- 退出 -->
    <van-button class="logout-btn" type="danger" @click="logout">退出</van-button>
  </div>
</template>

<script>
import { uploadAvatar } from "@/utils/api";
import { mapState, mapMutations } from "vuex";

export default {
  created() {
    // 调用 Vuex 中的 actions 中的 getUserInfo 方法获取用户数据
    this.$store.dispatch("getUserInfo");
  },
  // 计算属性
  computed: {
    ...mapState(["userInfo"])
  },
  methods: {
    ...mapMutations(["setPhoto"]),
    // 用户退出
    logout() {
      // 调用 Vuex 中 mutations 中的 logout 方法
      this.$store.commit("logout");
      // 跳转到首页
      this.$router.push("/");
    },
    // 上传图片
    async onAfterRead(file) {
      console.log(file);
      try {
        // 调用接口上传图片到服务器
        let res = await uploadAvatar(file.file);
        // 上传状态
        // file.status = 'uploading';
        // file.message = '上传中...';
        // 把新的头像路径更新到 Vuex 中
        this.setPhoto(res.data.data.photo)
        // 提示
        this.$toast.success('更新成功！')
        console.log(file);
      } catch (error) {
        this.$toast.fail('上传头像失败！')
      }
    }
  }
};
</script>

<style lang="less">
.my {
  padding-bottom: 50px;
  &-userinfo {
    padding-top: 50px;
    background-color: #3e9cfa;
    border-bottom: 1px solid #3e9cfa;
    .van-image {
      margin-right: 15px;
      border: 1px solid #fff;
    }
    .van-button {
      padding: 0 5px 0 5px;
    }
    .van-cell__title {
      color: white;
      font-size: 18px;
      padding-top: 10px;
    }
  }
  &-read {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 5px 12px;
    color: #fff;
    font-size: 12px;
    line-height: 1.4em;
    border-top-left-radius: 25px;
    border-bottom-left-radius: 25px;
    background-color: rgba(0, 0, 0, 0.2);
  }
  &-numinfo {
    display: flex;
    justify-content: space-around;
    padding: 15px;
    color: #fff;
    font-size: 13px;
    text-align: center;
    background-color: #3e9cfa;
  }
  .van-cell-group {
    border-top: 4px solid #f5f7f9;
  }
  .logout-btn {
    margin: 30px auto;
    display: block;
    width: 70%;
  }
  .van-cell::after {
    border-bottom: none;
  }
}
</style>